package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gitlab.local/backend/proto/dto"
	"manager/bll"
	"manager/lib/trace_log"
	"manager/model"
	SignalPo "manager/model/signal"
	"manager/server/web/middleware"
	"manager/utils"
)

var Lose = &lose{}

func init() {
	// 注册路由
	RegisterRouter(Lose)
}

type lose struct{}

// Init 初始化路由
func (a *lose) Init(r *gin.RouterGroup) {
	// web 路由
	g := r.Group("/lose", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
		g.POST("/unlock", a.unlock)
	}
	// app 路由
	appG := r.Group("app/lose", middleware.Auth())
	{
		appG.POST("/create", a.create)
		appG.POST("/update", a.update)
		appG.POST("/list", a.list)
		appG.POST("/delete", a.delete)
		appG.POST("/detail", a.find)
		appG.POST("/unlock", a.unlock)
	}
}

// create 创建
func (a *lose) create(c *gin.Context) {
	var (
		in  = &model.LoseCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	flag := 0
	bikeInfo, _ := bll.CacheBike.GetBikeInfoById(in.EbikeId)
	if info, _ := bll.Lose.Find(c, &model.LoseInfoRequest{
		EbikeId: &in.EbikeId,
		Status:  &flag,
	}); info != nil && info.Id > 0 {
		c.Error(fmt.Errorf("车辆已经上报失窃，请撤回失窃后再上报"))
		return
	}

	if err = bll.Lose.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		if bikeInfo.ID > 0 {
			trace_log.DebugLogger(bikeInfo.SNCode, "create", "添加失窃锁车失败 err:", err)
		}
		return
	}

	// 上报失窃进行锁车
	if bikeInfo.ID > 0 {
		bll.LoseLock.AddLoseLock(bikeInfo.SNCode)
		trace_log.DebugLogger(bikeInfo.SNCode, "AddLoseLock", "添加失窃锁成功")
	}

	utils.ResponseOk(c, nil)
}

// update 更新
func (a *lose) update(c *gin.Context) {
	var (
		in  = &model.LoseUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Lose.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list 列表查询
func (a *lose) list(c *gin.Context) {
	var (
		in  = &model.LoseListRequest{}
		out = &model.LoseListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Lose.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list 列表查询
func (a *lose) find(c *gin.Context) {
	var (
		in  = &model.LoseInfoRequest{}
		out = &model.LoseInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Lose.Find(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// delete 删除
func (a *lose) delete(c *gin.Context) {
	var (
		in  = &model.LoseDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Lose.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// unlock 解锁
func (a *lose) unlock(c *gin.Context) {
	var (
		in  = &model.LoseUnlockReq{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	bikeInfo, _ := bll.CacheBike.GetBikeInfoById(in.EbikeId)
	if bikeInfo.ID == 0 {
		c.Error(fmt.Errorf("解除失窃失败"))
		return
	}

	flag := 0
	recode, _ := bll.Lose.Find(c, &model.LoseInfoRequest{
		EbikeId: &bikeInfo.ID,
		Status:  &flag,
	})

	// 失窃中，但是车辆已锁定
	if recode.Id > 0 && recode.LockStatus == 1 {
		// 解除锁定
		err = bll.Signal.SendCmdToDevice(c, &SignalPo.CommandReq{
			EbikeId: bikeInfo.ID,
			Cmd:     int(dto.CmdType_CmdUnlock),
		})

		if err != nil {
			c.Error(fmt.Errorf("车辆离线中，解除失窃失败"))
			return
		}
	}

	// 解除锁定
	err = bll.LoseLock.RemoveLoseLock(bikeInfo.SNCode)
	if err != nil {
		c.Error(err)
		return
	}

	utils.ResponseOk(c, nil)
}
